<?php

namespace App\Logging;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Formatter\LineFormatter;

class Factory
{
    /**
     * 实现日志工厂
     *
     * @param  array  $config /config/logging.php 当前频道的配置
     * @return \Monolog\Logger
     */
    public function __invoke(array $config)
    {
        return new Logger('api', [
            $this->create($config, Logger::ERROR, '_errors'),
            $this->create($config, Logger::DEBUG),
        ]);
    }

    private function create($config, $level, $suffix = '') 
    {        
        $path = date('Y-m-d', time()).'/'.$config['prefix'].$suffix.'.log';
        if(PHP_OS == "WINNT"){
            $path = 'logs/'.$path;
            $path = storage_path($path);
        }else{
            //$path = '/data/logs/geeapi/'.$path;
            $path = '/var/log/geeapi/'.$path;
        }

        $handler = new StreamHandler($path, $level, false);
        $handler->setFormatter(new LineFormatter(null, 'Y-m-d H:i:s', false, true));

        return $handler;
    }
}